<template>
  <div class="social-signup-container">
    <div class="sign-btn" @click="wechatHandleClick('wechat')">
      <span class="wx-svg-container"><svg-icon icon-class="wechat" class="icon" /></span>
      WeChat
    </div>
    <div class="sign-btn" @click="qqHandleClick('qq')">
      <span class="qq-svg-container"><svg-icon icon-class="qq" class="icon" /></span>
      QQ
    </div>
    <div class="sign-btn" @click="githubHandleClick('github')">
      <span class="github-svg-container"><svg-icon icon-class="github" class="icon" /></span>
      GitHub
    </div>
  </div>
</template>

<script>
import store from '@/store'
import router from '@/router'

export default {
  name: 'ThirdPartLogin',
  methods: {
    wechatHandleClick(thirdpart) {
      alert('微信登录功能待开发……')
      // this.$store.commit('SET_AUTH_TYPE', thirdpart)
      // const appid = 'xxxxx'
      // const redirect_uri = encodeURIComponent('xxx/redirect?redirect=' + window.location.origin + '/auth-redirect')
      // const url = 'https://open.weixin.qq.com/connect/qrconnect?appid=' + appid + '&redirect_uri=' + redirect_uri + '&response_type=code&scope=snsapi_login#wechat_redirect'
      // openWindow(url, thirdpart, 540, 540)
    },
    qqHandleClick(thirdpart) {
      alert('QQ登录功能待开发……')
      // this.$store.commit('SET_AUTH_TYPE', thirdpart)
      // const client_id = 'xxxxx'
      // const redirect_uri = encodeURIComponent('xxx/redirect?redirect=' + window.location.origin + '/auth-redirect')
      // const url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' + client_id + '&redirect_uri=' + redirect_uri
      // openWindow(url, thirdpart, 540, 540)
    },
    githubHandleClick(thirdPart) {
      let flag = true
      window.addEventListener('message', function(e) {
        // e.data 就是后端颁发的token，执行vuex里面的方法 可以理解拿到了token 去请求获取用户信息的接口
        if (flag) {
          store.dispatch('user/loginByOauth', e.data).then((res) => {
            flag = false
            router.push({ path: '/' })
          }).catch(() => {
          })
        }
      }, false)
      const clientId = process.env.VUE_APP_GITHUB_CLIENT_ID
      const redirectUri = encodeURIComponent(process.env.VUE_APP_BASE_HTTP_API + '/oauth/github/login')
      const url = `https://github.com/login/oauth/authorize?client_id=${clientId}&state=STATE&redirect_uri=${redirectUri}`
      window.open(url, '_blank')
    }
  }
}
</script>

<style lang="scss" scoped>
  .social-signup-container {
    margin: 20px 0;

    .sign-btn {
      display: inline-block;
      cursor: pointer;
    }

    .icon {
      color: #fff;
      font-size: 24px;
      margin-top: 8px;
    }

    .wx-svg-container,
    .qq-svg-container,
    .github-svg-container {
      display: inline-block;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      padding-top: 1px;
      border-radius: 4px;
      margin-bottom: 20px;
      margin-right: 5px;
    }

    .wx-svg-container {
      background-color: #24da70;
    }

    .qq-svg-container {
      background-color: #6BA2D6;
      margin-left: 50px;
    }

    .github-svg-container {
      background-color: #1296db;
      margin-left: 60px;
    }
  }
</style>
